<template>
  <div class="">
    <div class="warp-box  clearfix">
      <div class="warp-con">
        <div class="header">
          <div class="headerWrapper">
            <div class="search">
              <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input @keyup.enter.native="query" auto-complete="off" class="input" placeholder="请输入名称"
                    type="text" v-model="searchFrom.name">
                    <i class="el-icon-close el-input__icon" slot="suffix" @click="searchFrom.name = ''"></i>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" style="margin-left: 5px" @click="query">查询</el-button>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="addDialog('add')">新增</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
        <div class="content">
          <el-table ref="multipleTable" border stripe :data="tableData" v-loading="loading"  :max-height="635"
                    element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)" :tooltip-effect="'dark'">
            <el-table-column align="center" prop="createDate" show-overflow-tooltip label="创建时间" />
            <el-table-column align="center" prop="name" show-overflow-tooltip label="名称" />
            <el-table-column align="center" prop="sortNo" show-overflow-tooltip label="排序" />
            <el-table-column label="操作" align="center" min-width="130" width="">
              <template slot-scope="scope">
                <span @click="updateDialog(scope.row.id)" class="pointer">修改</span>
                <i class="line"></i>
                <span @click="deleteTemplate(scope.row.id)" class="pointer">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="paging">
          <el-pagination :page-size="this.pageSize" :page-sizes="[10, 20, 50]" :total="tableDataTotalCount"
                         @current-change="handleCurrentChange" @size-change="handleSizeChange" background layout="total, sizes, prev, pager, next, jumper" />
        </div>
      </div>
      <el-dialog :close-on-click-modal="false" :title="detailEditType=='edit'?'修改采集模板' : '添加采集模板'" width="650px"
        :before-close="cancel" :visible.sync="dialogFormVisible">
        <el-form :model="addForm" :rules="rulesAdd" ref="ruleAddForm" class="demo-ruleForm" label-width="120px">
          <div class="form-table_col2">
            <table border="1" width="100%">
              <tr>
                <td class="form-item_label">名称</td>
                <td>
                  <el-input v-model="addForm.name" class="input" auto-complete="off"></el-input>
                </td>
              </tr>
              <tr>
                <td>采集组</td>
                <td>
                  <el-select filterable v-model="addForm.groupIds" multiple placeholder="请选择" clearable>
                    <el-option v-for="item in groupOption" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </td>
              </tr>
              <tr>
                <td>3d模型</td>
                <td>
                  <img v-if="addForm.modelType" style="cursor: pointer;" :src="loadImg(addForm.modelType)" @click.stop="selectIconShow = true">
                  <el-input v-else v-model="addForm.modelType" auto-complete="off" readonly @focus="selectIconShow = true"
                    @blur="inputBlur" ref="iconInput"></el-input>
                  <div v-show="selectIconShow" style="position: absolute;left: 0;top: -165px;z-index: 10000;width: 250px;height: 150px;
                    border: 1px solid #fff;background: rgba(14, 107, 183, 0.7);"
                    class="scroll" @mouseenter="selecting = true" @mouseleave="selecting = false">
                    <el-scrollbar class="page-component_scroll">
                      <el-tabs tab-position="left" style="height: 150px;">
                        <el-tab-pane v-for="(lib, i) in unityLibData" :key="i" v-if="lib.parentId==='0'" :label="lib.name">
                          <div v-for="(item, j) in unityLibData" :key="j" v-if="item.parentId===lib.id && item.tabName"
                            style="width: 50px;height: 50px;float: left;margin-top: 5px;cursor: pointer;" @click.stop="fileSelect(item.val)">
                            <img style="width: 45px;height: 45px;" :src="item.icon" />
                          </div>
                        </el-tab-pane>
                      </el-tabs>
                    </el-scrollbar>
                    <div class="triangle-down"></div>
                  </div>
                </td>
              </tr>
              <tr>
                <td>排序</td>
                <td>
                  <el-input-number v-model="addForm.sortNo" :min="1" class="input" auto-complete="off"></el-input-number>
                </td>
              </tr>
            </table>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="detailEditType == 'edit' || detailEditType == 'editType'" class="dialogBtn" type="primary"
            @click="updateTemplate('ruleAddForm')">修 改</el-button>
          <el-button v-else type="primary" @click="addTemplate('ruleAddForm')" class="dialogBtn">添 加</el-button>
          <el-button type="primary" @click="cancel" class="dialogBtn">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  import templateApi from '@/api/templateApi'
  import SelectTree from '@/components/TreeSelect'

  export default {
    components: {
      SelectTree
    },
    data() {
      return {
        loading: false,
        searchFrom: {
          name: ''
        },
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        TableDataTotalCount: '',
        dialogFormVisible: false,
        detailEditType: 'detail', // 'edit'
        addForm: {},
        selectIconShow: false,
        selecting: false,
        iconList: [],
        unityLibData: [],
        rulesAdd: {
          name: [{
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }]
        },
        module: 'TEMPLATE',
        groupOption: []
      }
    },
    computed: {
      tableDataTotalCount() {
        return this.TableDataTotalCount ? this.TableDataTotalCount : 0
      }
    },
    created() {
      this.findTemplatePage()
      this.findUnityLibData()
    },
    mounted() {},
    methods: {
      query() {
        this.pageIndex = 1;
        this.findTemplatePage();
      },
      loadImg(val) {
        for (let i = 0; i < this.unityLibData.length; i++) {
          let lib = this.unityLibData[i]
          if (lib.val === val) {
            return lib.icon
          }
        }
      },
      inputBlur() {
        if (this.selecting === false) {
          this.selectIconShow = false
        }
      },
      fileSelect(val) {
        this.addForm.modelType = val
        this.selectIconShow = false
      },
      findUnityLibData() {
        templateApi.findUnityLibData().then((res) => {
          if (res.data.messageType === '0') {
            this.unityLibData = res.data.result
            for (let i = 0; i < this.unityLibData.length; i++) {
              let lib = this.unityLibData[i]
              if (lib.icon) {
                lib.icon = process.env.FILE_SERVER + 'modelIcon/' + lib.icon
              }
            }
          }
        })
      },
      findTemplatePage() {
        this.loading = true
        templateApi.findTemplatePage({
          name: this.searchFrom.name,
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          module: this.module,
          sort: 'sortNo',
          order: 'ASC'
        }).then((res) => {
          this.loading = false
          if (res.data.messageType === '0') {
            this.tableData = res.data.result.rows
            this.TableDataTotalCount = res.data.result.totalCount
          }
        })
      },
      addDialog() {
        this.findGroup();
        this.detailEditType = 'add';
        this.addForm.sortNo = this.TableDataTotalCount + 1;
        this.dialogFormVisible = true;
        this.selectIconShow = false;
      },
      addTemplate(formName) {
        if (!this.addForm.name) {
          this.$message({
            message: '请输入名称',
            type: 'warning'
          })
          return false
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.showLoading()
            this.addForm.module = this.module
            let params = Object.assign({}, this.addForm)
            templateApi.addTemplate(params).then((res) => {
              this.hideLoading()
              if (res.data.messageType === '0') {
                this.$notify({
                  title: '消息',
                  message: ('添加成功!'),
                  position: 'bottom-right',
                  showClose: false,
                  type: 'success'
                })
                this.cancel()
                this.findTemplatePage()
              }
            })
          } else {
            return false
          }
        })
      },
      updateTemplate(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.showLoading()
            let params = this.addForm
            templateApi.updateTemplate(params).then((res) => {
              this.hideLoading()
              if (res.data.messageType === '0') {
                this.$notify({
                  title: '消息',
                  message: ('修改成功!'),
                  position: 'bottom-right',
                  showClose: false,
                  type: 'success'
                })
                this.cancel()
                this.findTemplatePage()
              }
            })
          } else {
            return false
          }
        })
      },
      updateDialog(param) {
        this.findGroup()
        this.detailEditType = 'edit'
        this.dialogFormVisible = true
        this.selectIconShow = false
        templateApi.getTemplate(param).then((res) => {
          if (res.data.messageType === '0') {
            this.addForm = res.data.result
          }
        })
      },
      deleteTemplate(param) {
        let tips = '确定要删除吗？'
        this.$confirm(tips, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.showLoading()
          templateApi.deleteTemplate(param).then((res) => {
            this.hideLoading()
            if (res.data.messageType === '0') {
              this.$notify({
                title: '消息',
                message: ('删除成功！'),
                position: 'bottom-right',
                showClose: false,
                type: 'success'
              })
              this.findTemplatePage()
            }
          })
        }).catch((error) => {
          console.log(error)
        })
      },
      findGroup() {
        templateApi.findTemplate('GROUP').then((res) => {
          if (res.data.messageType === '0') {
            this.groupOption = res.data.result
          }
        })
      },
      // 当前页
      pager(val) {
        this.pageIndex = val
        this.findTemplatePage()
      },
      cancel() {
        this.addForm = {}
        this.groupOption = []
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.tableData = []
        this.findTemplatePage()
      },
      handleCurrentChange(val) {
        this.pageIndex = val
        this.tableData = []
        this.findTemplatePage()
      },
      showLoading() {
        this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      },
      hideLoading() {
        this.$loading().close()
      }
    }
  }
</script>
<style lang="less" scoped>
  .header {
    height: 60px;

    .headerWrapper {
      display: inline-flex;
    }

    .search {
      font-size: 22px;
      font-weight: 400;
      color: rgba(255, 255, 255, 1);

      .input {
        width: 243px;
        height: 47px;
        background: rgba(0, 0, 0, 0);
        font-size: 18px;
        color: #ffffff;
      }
    }
  }

  .container {
    padding-left: 94px;
    padding-right: 94px;
  }

  .demo-ruleForm {
    & /deep/ .el-input--suffix .el-input__inner {
      padding-right: 15px;
    }
  }

  .f-g-sd1 {
    position: relative;
    float: left;
    width: 95px;
    margin-right: -95px;
  }

  .f-g-mn1 {
    float: right;
    width: 100%;
  }

  .f-g-mn1c {
    margin-left: 100px;
  }

  .detail-body {
    color: rgba(250, 250, 250, 0.9);

    &::after {
      content: '';
      display: block;
      visibility: hidden;
      height: 0;
      clear: both;
    }
  }

  .item-inline {
    width: 50%;
    float: left;
    margin-bottom: 20px;

    .item-name {
      display: inline-block;
    }

    .item-context {
      display: inline-block;
    }
  }

  .no-data {
    text-align: center;
    font-size: 14px;

    .tips {
      padding: 20px 0px;
      color: #666;
    }
  }

  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }

  .footer {
    margin-top: 40px;
    color: #ffffff;
    font-size: 16px;

    .info {
      .select {
        width: 52px;
        height: 35px;
      }

      .select /deep/ .el-input__inner {
        padding-left: 5px;
        padding-right: 0;
      }

      .select /deep/ .el-input__icon {
        width: 15px;
      }

      .select /deep/ .el-select__caret {
        /*transform:rotate(270deg);*/
        /*color: black;*/
      }

      .select /deep/ .el-select__caret.is-reverse {}
    }

    .paging {
      /*background-color: pink;*/
      text-align: right;
      padding-right: 30px;
      margin-top: -20px;

      & /deep/ .el-pagination.is-background .btn-prev {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination.is-background .btn-next {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pager li {
        background: none;
        border: 1px solid rgba(0, 255, 255, 1);
      }

      & /deep/ .el-pagination__total,
      & /deep/ .el-pagination__sizes {
        float: left;
        font-size: 1.6rem;
        height: 35px;
        line-height: 35px;
      }

      & /deep/ .el-input__inner {
        /*width:52px;*/
        height: 35px;
      }

      & /deep/ .el-input__icon {
        /*width: 20px;*/
        /*line-height: 3;*/
      }

      & /deep/ .el-input .el-input__inner {
        padding-right: 20px;
        color: #ffffff;
        font-size: 1.6rem;
      }
    }
  }

  .content {
    & /deep/ .el-checkbox__inner {
      height: 20px;
      width: 20px;
      font-size: 28px;
      font-weight: 400;
      color: rgba(255, 255, 255, 1);
      background: none;
    }

    & /deep/ .el-tooltip__popper {
      background-color: red;
    }

    & /deep/ .el-checkbox__inner::after {
      border-width: 2px;
      height: 6px;
      left: 6px;
      top: 3px;
      width: 4px;
    }

    & /deep/ .el-checkbox__input.is-indeterminate .el-checkbox__inner::before {
      top: 8px;
      background: white;
    }
  }

  .line {
    display: inline-flex;
    height: 21px;
    width: 1px;
    background: rgba(0, 255, 255, 1);
    margin: 0 10px;
    vertical-align: middle;
  }

  .dialogBtn {
    font-size: 1.6rem;

    &:hover {
      background: rgba(12, 161, 151, 1);
    }
  }
</style>
<style lang="less">
  /**
   * 修改样式   覆盖默认的饿了么全局样式
   */
  .el-form-item__label {
    padding: 0 20px 0 0 !important;
  }

  .scroll .page-component_scroll {
    height: 100% !important;
  }

  .scroll .page-component_scroll .page-scrollba_wrap {
    overflow-y: auto !important;
  }

  .scroll .el-scrollbar__wrap {
    overflow-x: hidden !important;
  }
</style>
